package com.nervenets.web.controller.admin;

import com.nervenets.web.controller.BaseController;
import com.nervenets.web.controller.IdParams;
import com.nervenets.web.controller.IdTokenParams;
import com.nervenets.web.controller.PagingParams;
import com.nervenets.web.entity.KeyValue;
import com.nervenets.web.entity.MessageCode;
import com.nervenets.web.hibernate.dao.NoticeDao;
import com.nervenets.web.hibernate.domain.Notice;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.ConstraintViolation;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Api(value = "公告接口v1", description = "公告相关接口")
@RestController
@RequestMapping("/api/v1/notice")
public class NoticeController extends BaseController {
    @Resource
    private NoticeDao noticeDao;

    @ApiOperation("新建或修改公告")
    @PostMapping("/edit")
    public String edit(@RequestBody NoticeEditParams params) {
        final Set<ConstraintViolation<NoticeEditParams>> errors = validator.validate(params);
        if (!errors.isEmpty()) {
            return errorMessage(errors);
        }

        Notice notice = new Notice(params.getTitle(), params.getContent());
        if (params.getNoticeId() > 0) {
            notice = noticeDao.findById(params.getNoticeId()).orElse(null);
            if (null == notice) return errorMessage(MessageCode.code_404, "您修改的公告ID不存在");
        }

        noticeDao.save(notice);

        return successMessage(notice);
    }

    @ApiOperation("后台用户账号列表")
    @PostMapping("/list")
    public String list(@RequestBody PagingParams params) {
        final Set<ConstraintViolation<PagingParams>> errors = validator.validate(params);
        if (!errors.isEmpty()) {
            return errorMessage(errors);
        }

        Page<Notice> page = noticeDao.findAll(PageRequest.of(params.getPage(), params.getLimit(), new Sort(Sort.Direction.DESC, "id")));
        List<NoticeModel> list = new ArrayList<>();
        page.map(notice -> {
            list.add(new NoticeModel(notice));
            return notice;
        });
        return successMessage(list, new KeyValue("page", params.getPage()), new KeyValue("total", page.getTotalElements()));
    }

    @ApiOperation("公告详情")
    @PostMapping("/detail")
    public String detail(@RequestBody IdParams params) {
        final Set<ConstraintViolation<IdParams>> errors = validator.validate(params);
        if (!errors.isEmpty()) {
            return errorMessage(errors);
        }

        Notice notice = noticeDao.findById(params.getTargetId()).orElse(null);
        if (null == notice) return errorMessage(MessageCode.code_404, "您要查看的公告ID不存在");
        return successMessage(notice);
    }

    @ApiOperation("删除公告")
    @PostMapping("/delete")
    public String delete(@RequestBody IdTokenParams params) {
        final Set<ConstraintViolation<IdTokenParams>> errors = validator.validate(params);
        if (!errors.isEmpty()) {
            return errorMessage(errors);
        }

        Notice notice = noticeDao.findById(params.getTargetId()).orElse(null);
        if (null == notice) return errorMessage(MessageCode.code_404, "您要删除的公告ID不存在");
        noticeDao.delete(notice);
        return successMessage();
    }
}
